What is gaxios?
The gaxios npm package is a lightweight HTTP client based on Axios but with a smaller footprint. It is designed to work in both browser and node environments, providing a simple way to make HTTP requests. It supports all HTTP request methods, automatic JSON data transformation, and custom configuration for requests.
What are gaxios's main functionalities?
GET Request
This feature allows you to make GET requests to retrieve data from a specified resource.
const { request } = require('gaxios');
async function getUser() {
try {
const response = await request({ url: 'https://api.example.com/user', method: 'GET' });
console.log(response.data);
} catch (error) {
console.error(error);
}
}
getUser();
POST Request
This feature allows you to make POST requests to send data to a server to create/update a resource.
const { request } = require('gaxios');
async function createUser(userData) {
try {
const response = await request({
url: 'https://api.example.com/user',
method: 'POST',
data: userData
});
console.log(response.data);
} catch (error) {
console.error(error);
}
}
createUser({ name: 'New User', email: 'newuser@example.com' });
Interceptors
Interceptors allow you to run your code or modify the request and/or response before the request is sent or after the response is received.
const { Gaxios } = require('gaxios');
const instance = new Gaxios({
baseURL: 'https://api.example.com'
});
// Add a request interceptor
instance.interceptors.request.use(config => {
// Do something before request is sent
config.headers['Authorization'] = 'Bearer token';
return config;
});
// Add a response interceptor
instance.interceptors.response.use(response => {
// Do something with response data
return response;
}, error => {
// Handle error
return Promise.reject(error);
});
Custom Configuration
Custom configuration allows you to specify various options for the HTTP request, such as headers, query parameters, timeout, and more.
const { request } = require('gaxios');
async function getCustomData() {
try {
const response = await request({
url: 'https://api.example.com/data',
method: 'GET',
timeout: 5000,
headers: { 'X-Custom-Header': 'foobar' }
});
console.log(response.data);
} catch (error) {
console.error(error);
}
}
getCustomData();
Other packages similar to gaxios
axios
Axios is a popular HTTP client for the browser and node.js. It is similar to gaxios but has a larger footprint and more features, such as interceptors, automatic transforms for JSON data, and client-side support for protecting against XSRF.
node-fetch
node-fetch is a light-weight module that brings the Fetch API to Node.js. It is similar to gaxios in terms of being promise-based and lightweight but does not have built-in support for interceptors or automatic JSON data transformation.
got
Got is a human-friendly and powerful HTTP request library for Node.js. It offers stream support, promises, and advanced features like retries and timeouts. It is more feature-rich compared to gaxios but also has a larger footprint.
superagent
Superagent is a small progressive client-side HTTP request library, and Node.js module with the same API, sporting many high-level HTTP client features. It is similar to gaxios but includes more built-in features like form data handling and file uploads.
gaxios
An HTTP request client that provides an axios
like interface over top of node-fetch
.
Install
$ npm install gaxios
Example
const {request} = require('gaxios');
const res = await request({
url: 'https://www.googleapis.com/discovery/v1/apis/'
});
Setting Defaults
Gaxios supports setting default properties both on the default instance, and on additional instances. This is often useful when making many requests to the same domain with the same base settings. For example:
const gaxios = require('gaxios');
gaxios.instance.defaults = {
baseURL: 'https://example.com'
headers: {
Authorization: 'SOME_TOKEN'
}
}
gaxios.request({url: '/data'}).then(...);
Request Options
{
url: string,
method: 'GET',
baseURL: 'https://example.com';
headers: { 'some': 'header' },
data: {
some: 'data'
},
maxContentLength: 2000,
maxRedirects: 100,
params: {
querystring: 'parameters'
},
paramsSerializer: (params) => {
return qs.stringify(params);
},
timeout: 1000,
adapter?: (options) => {
return {
data: 'your data'
}
};
responseType: 'json',
agent: someHttpsAgent,
validateStatus: (status: number) => true,
retryConfig: {
retry?: number;
currentRetryAttempt?: number;
httpMethodsToRetry?: string[];
statusCodesToRetry?: number[][];
onRetryAttempt?: (err: GaxiosError) => Promise<void> | void;
shouldRetry?: (err: GaxiosError) => Promise<boolean> | boolean;
noResponseRetries?: number;
},
retry: boolean,
signal?: AbortSignal
}
License
Apache-2.0